package com.blitscreen.render 
{
	import flash.display.BitmapData;
	import flash.filters.BitmapFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import com.bourre.collection.ArrayIterator;
	import com.bourre.collection.Iterable;
	import com.bourre.collection.Iterator;		

	/**
	 * @author Cedric Nehemie
	 */
	public class BasicPostProcessPass implements PostProcessPass, Iterable
	{	
		protected var _nLastPostProcessTime : Number;	
		
		protected var _aPostProcessFilters : Array;

		public function BasicPostProcessPass ()
		{
			_aPostProcessFilters = new Array( );
			_nLastPostProcessTime = 0;
		}

		public function render (screen : BitmapData) : void
		{
			var ms : Number = getTimer();
			var i : Iterator = iterator();
			
			while( i.hasNext() )
			{
				screen.applyFilter( screen, 
									new Rectangle( 0, 0, screen.width, screen.height ),
									new Point(), 
									i.next() as BitmapFilter );
			}
			_nLastPostProcessTime = getTimer() - ms;
		}

		public function add ( filter : BitmapFilter ) : void
		{
			if( !contains( filter ) )
			{
				_aPostProcessFilters.push( filter );
			}
		}	
		public function addAt ( index : uint, filter : BitmapFilter ) : void
		{
			if( index < size() )
			{
				_aPostProcessFilters.splice( index, 0, filter );
			}
		}	
		public function remove ( filter : BitmapFilter ) : void
		{
			if( contains( filter ) )
			{
				_aPostProcessFilters.splice( _aPostProcessFilters.indexOf( filter ), 1 );
			}
		}
		public function removeAt ( index : uint ) : void
		{
			if( index < size() )
			{
				_aPostProcessFilters.splice( index, 1 );
			}
		}		
		public function removeAll () : void
		{
			_aPostProcessFilters = new Array();
		}
		public function hasPostProcessFilters () : Boolean
		{
			return _aPostProcessFilters.length != 0;
		}	
		public function getPostProcessFilters () : Array
		{
			return _aPostProcessFilters;
		}
		public function contains (filter : BitmapFilter) : Boolean
		{
			return _aPostProcessFilters.indexOf( filter ) != -1;
		}
		public function size () : Number
		{
			return _aPostProcessFilters.length;
		}		
		public function iterator () : Iterator
		{
			return new ArrayIterator( _aPostProcessFilters );
		}

		public function getLastPostProcessTime () : Number
		{
			return _nLastPostProcessTime;
		}
	}
}
